<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标题">
          <a-input v-decorator="[ 'title',validatorRules.title]" placeholder="请输入标题" />
        </a-form-item>
        <a-form-item label="海报宣传类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <cn-dict-select-tag
            v-model='dataType'
            placeholder="请选择"
            dict-code="factory_type"
            @change="dotype"
            :triggerChange="true"
            :options="{value:'dictDataCode',label:'dictDataDesc'}"
            v-decorator.trim="[ 'dataType', validatorRules.factoryType]"/>
        </a-form-item>
        <a-form-item label="用户类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <cn-dict-select-tag
            placeholder="请选择"
            dict-code="user_type"
            triggerChange
            :options="{value:'dictDataCode',label:'dictDataDesc'}"
            v-decorator.trim="[ 'userType', validatorRules.userType]"/>
        </a-form-item>
        <a-form-item label="资料类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <cn-dict-select-tag
            placeholder="请选择"
            dict-code="material_type"
            triggerChange
            :options="{value:'dictDataCode',label:'dictDataDesc'}"
            v-decorator.trim="[ 'materialType', validatorRules.materialType]"/>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="发布组织" >
          <j-tree-select placeholder="请选择" v-decorator="['orgName', {}]" data-type="org" v-model="orgName" />
        </a-form-item>
<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="发布用户">-->
<!--          <j-tree-select placeholder="请选择被指定人" v-decorator="['appointorCode', {}]" :trigger-change="true"></j-tree-select>-->
<!--        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="开始时间">
          <a-date-picker format="YYYY-MM-DD" valueFormat="YYYY-MM-DD" v-decorator="['startTime', validatorRules.startTime]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="结束时间">
          <a-date-picker format="YYYY-MM-DD" valueFormat="YYYY-MM-DD" v-decorator="[ 'endTime',validatorRules.endTime]"/>
        </a-form-item>

        <a-form-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-image-upload class="avatar-uploader" text="上传" v-decorator="[ 'urls', validatorRules.urls]"></j-image-upload>
<!--          <j-upload   @change="pdLogoChange" accept='.png,.jpg,.jpeg' :oldList="urlList" fileType='image' :number='10'-->
<!--                      class='avatar-uploader' text='上传' :disabled='disableSubmit' :returnUrl='false'-->
<!--                      v-decorator="[ 'urls',validatorRules.urls]">-->
<!--          ></j-upload>-->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="内容">
          <a-textarea placeholder="请输入内容" maxlength='100' v-decorator="['remarks', {}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction,getAction,postAction} from '@/api/manage'
import pick from 'lodash.pick'
import JImageUpload from '@/components/cn-form/JImageUpload'
import JUpload from '@/components/cn-form/JUpload'
export default {
  name: 'SfaPicFactoryModal',
  components: {JImageUpload , JUpload},
  data () {
    return {
      title:"操作",
      dataType : '',  //海报类型
      // urlList:[
      //   {
      //     name:'',
      //     url:'',
      //     uid:''
      //   }
      // ],
      urls:[],
      visible: false,
      selecttype:'',
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules:{
        materialType:{ rules: [{
            required: true, message: '请选择资料类型!'
          }]},
        factoryType:{ rules: [{
            required: true, message: '请选择类型!'
          }]},
        // userType: {
        //   rules: [{
        //     required: true, message: '请选择用户类型!'
        //   }]
        // },
        startTime: {
          rules: [{
            required: true, message: '请选择开始时间!'
          }]
        },
        endTime: {
          rules: [{
            required: true, message: '请选择结束时间!'
          }]
        },
        urls: {
          rules: [{
            required: true, message: '请上传海报!'
          }]
        },
        title: {
          rules: [{
            pattern: /^[\u4e00-\u9fa5_a-zA-Z0-9]{4,64}$/,
            required: true, message: '请输入4~64位内容为中文，英文字母和数字及下划线的字符!'
          }]
        },
        status: {
          rules: [{
            required: true, message: '请选择状态!'
          }]
        }
      },
      url: {
        add: window._CONFIG['domianSfaURL']+ "/picfactory/saveOrUpdate",
        // info: window._CONFIG['domianSfaURL']+ "/picfactory/getInfoById",
        queryById: '/picfactory/getInfo',
      },
    }
  },
  methods: {
    // //添加图片
    // pdLogoChange(e){
    //   this.urlList = []
    //   this.urls = []
    //   e.forEach(item=> {
    //     this.urlList.push({url:item.url,name:item.name,uid:item.uid,status:'done'})
    //   })
    //   console.log(this.urlList)
    //   for(let i = 0;i<this.urlList.length;i++){
    //     this.urls.push(this.urlList[i].url)
    //   }
    //   console.log(this.urls)
    // },
    dotype(val){
      this.selecttype = val
    },
    add(){
      this.edit({});
    },
    edit (record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      // this.model.isRoll = this.model.isRoll+"";
      // this.model.isControl = this.model.isControl+"";
      this.urls = this.model.urls
      console.log(this.urls)
      // this.getImgUrls(record)
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model,'remarks','startTime','endTime',
          'status', 'title', 'userType','appointorCode','orgCode','orgName','dataType','materialType','urls'))
      });
    },
    getImgUrls(record){
      var that = this
      let formData = Object.assign({}, record);
      // getAction(this.url.info,formData,'get').then((res)=>{
      //   if(res.success){
      //     that.model.urls = res.result
      //     this.$nextTick(() => {
      //       this.form.setFieldsValue(pick(that.model,'urls'))
      //     });
      //   }else{
      //   }
      // }).finally(() => {
      //
      // })
    },
    close () {
      this.$emit('close');
      this.visible = false;
      this.urls = []
      this.urlList = []
    },
    handleOk () {
      console.log(this.urls)
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          httpurl += this.url.add;
          let method = '';
          method = 'post';
          let formData = Object.assign(this.model, values);
          console.log(formData,this.model.urls)
          formData.urls = []
          formData.urls.push(this.form.urls)
          // formData.orgName = this.orgName
          // console.log(this.orgName)
          console.log(formData.urls)
          //时间格式化
          httpAction(httpurl,formData,method).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })
        }
      })
    },
    handleCancel () {
      this.close()
    },
    onChange(value){
      if(!value){
        this.treeValue = ''
        this.form.typeCode = ''
      } else {
        this.treeValue = value
        this.form.typeCode = value
      }
    },
  }
}
</script>

<style scoped>

</style>